﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Qios.DevSuite.Components;
using BusinessObject;
using YamahaTuanTruc.AuthenticationService;

namespace YamahaTuanTruc
{
    public partial class FrmLogin : Qios.DevSuite.Components.Ribbon.QRibbonForm
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            txtUsername_Leave(null, null);
            txtPassword_Leave(null, null);
            if (string.IsNullOrEmpty(errorProvider.GetError(txtUsername)) && string.IsNullOrEmpty(errorProvider.GetError(txtPassword)))
            {
                Authenticate(txtUsername.Text, txtPassword.Text);
            }
        }

        private void Authenticate(string username, string password)
        {
            KhachHang kh = new KhachHang();
            kh.DienThoai = "0932093019";
           
            //if (!IsAuthenticated(username, password))
            //{
                //return;
            //}
            
            Program.User = new User("giangdinhtt");
            Hide();
            new FrmMain().ShowDialog();
        }

        private bool IsAuthenticated(string username, string password)
        {
            
            bool result = false;

            try
            {
                using (var svc = new AuthenticationServiceClient())
                {
                    result = svc.Authentication(username, password);
                }
            }
            catch (System.Net.WebException wex)
            {               
                MessageBox.Show("Không thể kết nối tới máy chủ, vui lòng kiểm tra lại đường truyền hoặc liên hệ với admin để biết thêm chi tiết");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        {
            errorProvider.SetError(txtUsername, null);
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                errorProvider.SetError(txtUsername, "Bạn chưa điền tên đăng nhập");
            }
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            errorProvider.SetError(txtPassword, null);
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                errorProvider.SetError(txtPassword, "Bạn chưa điền mật khẩu");
            }
        }

        private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnExit_Click(sender, e);
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnExit_Click(sender, e);
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnExit_Click(sender, e);
            }
        }
    }
}